package com.linghu.ai.repository;

import com.linghu.ai.entity.LiveRoom;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 直播间Repository接口
 */
@Repository
public interface LiveRoomRepository extends JpaRepository<LiveRoom, Long> {
    
    /**
     * 根据直播间ID查找
     */
    Optional<LiveRoom> findByRoomId(String roomId);
    
    /**
     * 根据设备ID查找直播间
     */
    List<LiveRoom> findByDeviceId(Long deviceId);
    
    /**
     * 根据状态查找直播间
     */
    List<LiveRoom> findByStatus(LiveRoom.LiveRoomStatus status);
    
    /**
     * 查找活跃的直播间
     */
    @Query("SELECT lr FROM LiveRoom lr WHERE lr.status = 'ACTIVE'")
    List<LiveRoom> findActiveLiveRooms();
    
    /**
     * 根据设备MAC地址查找直播间
     */
    @Query("SELECT lr FROM LiveRoom lr JOIN lr.device d WHERE d.macAddress = :macAddress")
    List<LiveRoom> findByDeviceMacAddress(@Param("macAddress") String macAddress);
    
    /**
     * 检查直播间ID是否存在
     */
    boolean existsByRoomId(String roomId);
    
    /**
     * 根据URL查找直播间
     */
    Optional<LiveRoom> findByRoomUrl(String roomUrl);
} 